% Parameters for intensity channel 

%% input parameters
fpath = 'balloons.png'; %'pout.tif';
inputvideo = 'video.avi';
color_img = 1;  % flag to enable pre-processing if color-image

nr = 128;       %input image size - num of rows
nc = 128;       %input image size - num of cols

q = quantizer('fixed', [32,20]);    %input data representation (32 bits, 20 fraction bits)
input_file_generate = 1; % if generating input dat file is needed
datfileseed = 'frame-'; %'image_RminusG_256.dat';

%% algorithmic parameters


%% for comparing hardware result
%% pre-processing

  %  im1 = imread(fpath);
   % im2 = imresize(im1, [nr, nc]);
 
    mov = aviread(inputvideo) ; 
   for i = 1:2  %size(mov,2) %will go to number of frames
 
    datfileframe = strcat(int2str(i),'-');
    im2= mov(1,i).cdata;
    im2 = imresize(im2, [nr,nc]);   % resize to desired img size
    img = double(im2)./255 ;
    [I R G B Y] = SalColorMap(img);   % Generate I, RG and BY channels 
    
    img = I; %abs(R - G);   
    img3 = R-G;
    img4 = B-Y; 
    
        %-----------------Int
        img_q = quantize(q, img);
        %convert to column vector (row-major)
        img_1d = reshape(img_q', nr*nc, 1);
        
            % convert to hex
        img_1d_hex = num2hex(q, img_1d);
        img_1d_hex2dec= hex2dec(img_1d_hex);
        img_1d_hex2dec=uint32(img_1d_hex2dec);
        
             % Write into the file
        datfileframeint = strcat(datfileframe,'0','.bin');
        fid = fopen(datfileframeint, 'w+');
        for j=1:size(img_1d_hex2dec,1)
            % img_1d_hex2dec(j,1) =  1431677610;% 5555AAAA    % 1431655765 55555555;
         %   fprintf(fid, '%X\n',img_1d(j,:));
         % fprintf(fid,'%8s\n', img_1d_hex(j,:));
           fwrite(fid,img_1d_hex2dec(j,1),'uint32'); %'b'additional parameter reverses bit order;
        end
        fclose(fid);
        
        %------------------------Flickr
        if (i>1) 
            img_1d_f = img_1d -prev_int_img;
            img_1d_f_hex = num2hex(q,img_1d_f);
               img_1d_f_hex2dec= hex2dec(img_1d_f_hex);
         img_1d_f_hex2dec=uint32(img_1d_f_hex2dec);
             %flickr starts at frame 2
        % Write into the file
      
             datfileframeflickr = strcat(datfileframe,'0','.bin');
             fid = fopen(datfileframeflickr, 'w+');
             for j=1:size(img_1d_f_hex2dec,1)
               fwrite(fid,img_1d_f_hex2dec(j,1),'uint32');
             end
            fclose(fid);
        
        end
            
          
        %-------------RG
                rgimg_q = quantize(q, img3);
        %convert to column vector (row-major)
        img_1d_rg = reshape(rgimg_q', nr*nc, 1);
        % convert to hex
        img_1d_hex_rg = num2hex(q, img_1d_rg);
               img_1d_hex2dec_rg= hex2dec(img_1d_hex_rg);
         img_1d_hex2dec_rg=uint32(img_1d_hex2dec_rg);
        

        % Write into the file
        datfileframerg = strcat(datfileframe,'2','.bin');
        fid = fopen(datfileframerg, 'w+');
        for j=1:size(img_1d_hex2dec_rg,1)
            fwrite(fid,img_1d_hex2dec_rg(j,1),'uint32');
        end
        fclose(fid);
        
        %-------------BY
                byimg_q = quantize(q, img4);
        %convert to column vector (row-major)
        img_1d_by = reshape(byimg_q', nr*nc, 1);
        % convert to hex
        img_1d_hex_by = num2hex(q, img_1d_by);
        
          img_1d_hex2dec_by= hex2dec(img_1d_hex_by);
         img_1d_hex2dec_by=uint32(img_1d_hex2dec_by);

        % Write into the file
        datfileframeby = strcat(datfileframe,'3','.bin');
        fid = fopen(datfileframeby, 'w+');
        for j=1:size(img_1d_hex2dec_by,1)
           fwrite(fid,img_1d_hex2dec_by(j,1),'uint32');
        end
        fclose(fid);
        
        prev_int_img = img_1d ;
        fprintf(strcat('frame',i,' '))
        
   
   end 


